package test;

import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.net.URL;
import java.net.URLConnection;
import java.util.Random;

import org.apache.commons.codec.binary.Base64;
import org.apache.commons.codec.digest.DigestUtils;

public class RealThing {

	public static void main(String[] args)
	{
		try
		{
			Credentials creds = new Credentials();
			String email = creds.email;
			String authToken = creds.authToken;
			
			String urlString = "http://11870.com/api/v1";
			
			URLConnection c = new URL(urlString).openConnection();

			Base64 b64 = new Base64();
			String created = new Long(System.currentTimeMillis()).toString();
			String nonce = (new Long((new Random()).nextLong())).toString();

			String digest = new String(b64.encode(DigestUtils.sha(nonce + created + authToken)));

			c.addRequestProperty("Authorization", "WSSE realm=\"11870.com\", profile=\"UsernameToken\"");
			c.addRequestProperty("X-WSSE", "UsernameToken Username=\"" + email + "\", PasswordDigest=\"" + digest + "\", Nonce=\"" + nonce + "\", Created=\"" + created + "\"");

			
			BufferedReader resp = new BufferedReader( new InputStreamReader(c.getInputStream()) );
			String str;
			while( (str = resp.readLine()) != null ) {
				System.out.println( str );
		    }
		}
		catch (Exception e)
		{
			e.printStackTrace();
		}
	}
}
